package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.domain.HotelCatalog;
import cn.wolfcode.trip.base.query.HotelQueryObject;
import cn.wolfcode.trip.base.service.IHotelCatalogService;
import cn.wolfcode.trip.base.service.IHotelService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/hotelCatalog")
public class HotelCatalogController {

    @Autowired
    private IHotelService hotelService;
    @Autowired
    private IHotelCatalogService hotelCatalogService;
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")HotelQueryObject qo){
        PageInfo result = hotelCatalogService.query(qo);
        model.addAttribute("result",result);
        model.addAttribute("places",regionService.list());
        model.addAttribute("hotels",hotelService.list());
        return "hotelCatalog/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(HotelCatalog entity, MultipartFile img){
        //判断有没有设置封
        if (img != null && img.getSize() > 0 && img.getContentType().startsWith("image")){
            //把图片上传到本地图库,返回图片路径
            String url = UploadUtil.upload(img);
            //替换以前默认的封面
            entity.setCoverUrl(url);
        }
        hotelCatalogService.saveOrUpdate(entity);
        return "redirect:/hotelCatalog/list.do";
    }

    //,produces = "text/html;charset=utf-8"
    @RequestMapping(value = "/getContentById")
    @ResponseBody
    public Object getContentById(Long id){
        //String s = hotelCatalogService.listById(id);
        //System.out.println(s);
        //"<p><img alt="" src="/upload/74d0c323-e64b-49a2-91bd-90892a7bc4d5.png" style="height:359px; width:361px" /></p>"
        //return s;
        JSONResult result = new JSONResult();
        try{
            String s = hotelCatalogService.listById(id);
            result.setData(s);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }
}
